﻿using Ninject;
using ProgressTracker.Dependencies;
using ProgressTracker.WebUI.Translation;
using System;
using System.Web.Mvc;
using System.Web.Routing;

namespace ProgressTracker.WebUI.Infrastructure
{
    public class NinjectControllerFactory : DefaultControllerFactory
    {
        private IKernel _ninjectKernel;

        public NinjectControllerFactory()
        {
            _ninjectKernel = new StandardKernel();
            LoadModules();
            AddBindings();
        }

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            return controllerType == null ? null : (IController)_ninjectKernel.Get(controllerType);
        }

        private void LoadModules()
        {
            _ninjectKernel.Load(new AccountModule());
            _ninjectKernel.Load(new DataModule());
            _ninjectKernel.Load(new DomainModule());
            _ninjectKernel.Load(new EmailModule());
        }

        private void AddBindings()
        {
            //TODO: Should probably make these their own module
            _ninjectKernel.Bind<IDayModelMapper>().To<DayModelMapper>();
            _ninjectKernel.Bind<ITaskModelMapper>().To<TaskModelMapper>();
            _ninjectKernel.Bind<IAuthentication>().To<FormsAuthenticationWrapper>();
        }
    }
}